
<template>
    <div style="width:100%;height:100%;position:absolute;left:0;top:0;opacity:0">
        <input style="width:100%;height:100%;position:absolute;left:0;top:0;opacity:0;display:block;z-index:500;"
               type="file"
               :id="id"
                name="myphoto"
                accept="image/*"
                multiple="multiple"
               ref="new_company_image">
    </div>
</template>
<script>
import {uploadImg } from '../api/apis/home.js';
export default {
    props:{
        // 限制上传图片的大小
        imgSize:{
            type:String,
            default:'500'
        },
        // 限制上传图片的宽高 1正方形 0长方形 2不限制宽度
        imgArea:{
            type:Number,
            default:0
        },
        // 图片索引
        index:Number,
        // id
        id:{
            type:String,
            default:'saveImage'
        }

    },
    data() {
        return {
            loading:null

        }
    },
    created() {
      
    },
    watch: {
        uplodUrl:function(val,old){
            if(val){
           
            }
        }
    },
    mounted() {
        this.great();
    },
    methods: {
        great: function () {
            var that_ = this,
                saveImage = document.getElementById(this.id);
              console.log('////')
            saveImage.onchange = function () {
                console.log(this.files[0])
                that_.loading = that_.$loading({
                    lock: true,
                    text: '正在上传图片…',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                var imgFile = this.files[0];
                var fr = new FileReader();
                fr.onload = function () {
                    let img = new Image()
                    img.src = fr.result
                    img.onload = function () {
                        saveImage.setAttribute("type",'text');
                        that_.getUploadImg(fr.result, that_,saveImage);
                    };
                
                }
                fr.readAsDataURL(imgFile);
            }
        },
        // 上传图片 http://192.168.1.114:8084/nbtjy/crm/image/upload
        getUploadImg(file, that_,saveImage) {
            let params = {
                base64File: file
            }
            uploadImg(params).then(res=>{
                const {success,result} = res;
                if(success){
                    that_.$emit('statusFor',result,this.index);
                    that_.loading.close();
                }
                saveImage.setAttribute("type",'file');
            })
        }
    }
}
</script>